package com.zbkc.util;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.os.Handler;
import android.widget.Toast;

import com.zbkc.dialog.LoadingDialog;

public class AlertUtils {

	static Handler handler = new Handler();

	public static void toast(final Context ctx, final String txt) {
		handler.post(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(ctx, txt, Toast.LENGTH_SHORT).show();
			}
		});
	}

	static LoadingDialog loadingDialog;

	/**
	 * 弹出加载框
	 * 
	 * @param ctx
	 * @param msg
	 */
	public static void showLoadingDialog(final Context ctx, final String msg,
			final boolean canceable) {
		handler.post(new Runnable() {
			@Override
			public void run() {
				if (ctx == null) {
					return;
				}

				if (loadingDialog == null) {
					loadingDialog = new LoadingDialog(ctx);

					loadingDialog.setOnCancelListener(new OnCancelListener() {
						@Override
						public void onCancel(DialogInterface dialog) {
							loadingDialog = null;
						}
					});
					loadingDialog.setOnDismissListener(new OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dialog) {
							loadingDialog = null;
						}
					});

				}

				loadingDialog.setCancelable(canceable);
				loadingDialog.setLoadingMessage(msg);
				loadingDialog.show();
			}
		});

	}

	public static void setLoadingDialogText(final String msg) {
		handler.post(new Runnable() {
			@Override
			public void run() {
				if (loadingDialog != null && loadingDialog.isShowing()) {
					loadingDialog.setLoadingMessage(msg);
				}
			}
		});

	}

	static final String TAG = AlertUtils.class.getSimpleName();

	/**
	 * 关闭加载框
	 */
	public static void dismissLoadingDialog() {
		handler.post(new Runnable() {
			@Override
			public void run() {
				if (loadingDialog != null) {
					loadingDialog.cancel();
				}
				loadingDialog = null;
			}
		});

	}

}
